﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoCountReName
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            textBox_Path.Text = Application.StartupPath;
        }

        private void btn_Brows_Click(object sender, EventArgs e)
        {
            if(folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox_Path.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void btn_Rename_Click(object sender, EventArgs e)
        {
            if(!Directory.Exists(textBox_Path.Text))
            {
                MessageBox.Show("路径不存在！请重新选择！");
                return;
            }
            if(string.IsNullOrWhiteSpace(comboBox_FileType.Text))
            {
                MessageBox.Show("请输入文件类型！");
                return;
            }
            string[] dirs = Directory.GetDirectories(textBox_Path.Text);
            string startLetter = comboBox_Format.Text[0].ToString();
            foreach(string dir in dirs)
            {
                int fileCount = 0;
                if (comboBox_FileType.Text.Contains("|"))
                {
                    string[] fileTypes = comboBox_FileType.Text.Split('|');
                    foreach(string fileType in fileTypes)
                    {
                        fileCount += Directory.GetFiles(dir, fileType).Length;
                    }
                }
                else
                {
                    fileCount += Directory.GetFiles(dir, comboBox_FileType.Text).Length;
                }

                string dirName = Path.GetFileName(dir);
                string dirParent = Path.GetDirectoryName(dir);
                if(dirName.Contains(startLetter))
                {
                    string newName = dirParent + "\\" + dirName.Substring(0, dirName.IndexOf(startLetter)) + string.Format(comboBox_Format.Text.Replace("*", "{0:G}"), fileCount);

                    try
                    {
                        Directory.Move(dir, newName);
                    }
                    catch { }
                }
                else
                {
                    string newName = dir + string.Format(comboBox_Format.Text.Replace("*", "{0:G}"), fileCount);
                    try
                    {
                        Directory.Move(dir, newName);
                    }
                    catch { }
                }
            }

            MessageBox.Show("重命名完成！","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
        }

        private void btn_Trim_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(comboBox_Format.Text))
            {
                MessageBox.Show("请输入格式类型！");
                return;
            }

            string[] dirs = Directory.GetDirectories(textBox_Path.Text);
            string startLetter = comboBox_Format.Text[0].ToString();
            foreach (string dir in dirs)
            {
                string dirName = Path.GetFileName(dir);
                string dirParent = Path.GetDirectoryName(dir);
                if (dirName.Contains(startLetter))
                {
                    string newName = dirParent + "\\" + dirName.Substring(0, dirName.IndexOf(startLetter));

                    try
                    {
                        Directory.Move(dir, newName);
                    }
                    catch { }
                }
            }
            MessageBox.Show("去除已全部完成！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
